import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutter_video_app/common/constants/message.dart';
import 'package:flutter_video_app/common/utils/navigator_helper.dart';
import 'package:flutter_video_app/model/home_page_model.dart';
import 'package:flutter_video_app/model/issue_model.dart';
import 'package:flutter_video_app/page/home/rank_widget_item.dart';
import 'package:flutter_video_app/page/home/video_search.dart';
import 'package:flutter_video_app/widgests/loading_container.dart';
import 'package:flutter_video_app/widgests/provider_widgest.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

//首页
class HomePage extends StatefulWidget {
  @override
  State createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int number = 8;
  List<String> items = ["1", "2", "3", "4", "5", "6", "7", "8"];
  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: _appBar(),
      body: _body(),
    );
  }

  AppBar _appBar() {
    return new AppBar(
      title: Text(
        Message.home_title,
        style: TextStyle(fontSize: 18, color: Colors.black),
      ),
      centerTitle: true,
      brightness: Brightness.light,
      actions: <Widget>[
        IconButton(
            icon: Icon(
              Icons.search,
              color: Colors.black45,
            ),
            onPressed: () {
              NavigatorHelper.to(VideoSearch());
            })
      ],
    );
  }

  Widget _body() {
    return new ProviderWidget<HomePageModel>(
        model: HomePageModel(),
        onModelInit: (model) => model.refresh(),
        builder: (context, model, child) {
          return LoadingContainer(
              child:
                  new SmartRefresher(
                enablePullDown: true,
                enablePullUp: true,
                header: WaterDropHeader(),
                footer: CustomFooter(
                  builder: (BuildContext context, LoadStatus mode) {
                    Widget body;
                    if (mode == LoadStatus.idle) {
                      body = Text("下拉刷新");
                    } else if (mode == LoadStatus.loading) {
                      body = CupertinoActivityIndicator();
                    } else if (mode == LoadStatus.failed) {
                      body = Text("加载失败,请重试");
                    } else if (mode == LoadStatus.canLoading) {
                      body = Text("释放加载更多");
                    } else {
                      body = Text("没有更多信息了");
                    }
                    return Container(
                      height: 55.0,
                      child: Center(child: body),
                    );
                  },
                ),
                controller: model.refreshController,
                onRefresh: () => model.refresh(),
                onLoading: () => model.loadMore(),
                child: _bodyContent(context,model,child),
              ),
              loading: model.loading);
          // return new Center(child: Text('sf'),);
        });
  }

  Widget _bodyContent(BuildContext context, HomePageModel model, Widget child) {
    return ListView.separated(
      itemBuilder: (context, index) {
        if (index == 0) {
          return _banner(context);
        } else {
          if (model.itemList[index].type =='textHeader') {
            return _titleItem(model.itemList[index]);
          }
          //构建推荐视频
          return RankWidgetItem(item: model.itemList[index]);
          // return Card(
          //   child: Text('这是视频位置'),
          // );
        }
      },
      separatorBuilder: (context, index) {
        return new Padding(
          padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
          child: Divider(
            color: Colors.grey,
            height: 0.5,
          ),
        );
      },
      itemCount: model.itemList.length,
    );
  }

 Widget _banner(BuildContext context) {
    HomePageModel model = Provider.of(context);
    return Container(
      height: 200,
      padding: EdgeInsets.only(left: 15, top: 15, right: 15),
      child: Stack(
        children: <Widget>[
          Swiper(
              autoplay: true,
              itemBuilder: (BuildContext context, int index) {
                return Hero(
                    tag:
                    '${model.bannerList[index].data.id}${model.bannerList[index].data.time}',
                    child: Container(
                      decoration: BoxDecoration(
                          image: DecorationImage(
                              image: CachedNetworkImageProvider(
                                  model.bannerList[index].data.cover.feed),
                              fit: BoxFit.cover),
                          borderRadius: BorderRadius.circular(4)),
                    ));
              },
              onIndexChanged: (index) {
                model.changeBannerIndex(index);
              },
              onTap: (index) {
                // NavigatorManager.to(VideoDetailPage(data: model.bannerList[index].data));
              },
              itemCount: model.bannerList?.length ?? 0,
              pagination: new SwiperPagination(
                  alignment: Alignment.bottomRight,
                  builder: DotSwiperPaginationBuilder(
                      activeColor: Colors.white, color: Colors.white24))),
          Positioned(
              width: MediaQuery.of(context).size.width - 30,
              bottom: 0,
              child: Container(
                  padding: EdgeInsets.fromLTRB(15, 10, 0, 10),
                  decoration: BoxDecoration(
                      color: Colors.black12,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(4),
                          bottomRight: Radius.circular(4))),
                  child: Text(model.bannerList[model.currentIndex].data.title,
                      style: TextStyle(color: Colors.white, fontSize: 12))))
        ],
      ),
    );
  }

  _titleItem(Item item) {
    return Container(
        decoration: BoxDecoration(color: Colors.white24),
        padding: EdgeInsets.only(top: 15, bottom: 5),
        child: Center(
            child: Text(item.data.text,
                style: TextStyle(
                    fontSize: 18,
                    color: Colors.black87,
                    fontWeight: FontWeight.bold))));
  }
}
